package com.example.myprojectoffeixue;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

//本界面是新设备入网界面，主要功能是为目标网关添加新设备，
// 主要逻辑是动画呈现，传递信息是用的handle机制；
public class AddSheBeiActivity extends Activity {
    private ImageView iv;
    private TextView tv_start;
    public ArrayList<String> mac;
    private TextView tv;
    private TextView tv_select;
    private TextView tv_select2;
    private TextView tv_selected;
    private Animation la;
    private boolean flag1;
    private MyApplication m;

   /* Handler hand = new Handler() {

        public void handleMessage(android.os.Message msg) {
            if (msg.what == 25) {
                String devicId= (String) msg.obj;
                String s_msg=null;
                if ("0100".equals(devicId)) {
                   s_msg="存在开关入网";
                } else if ("010B".equals(devicId)||"010A".equals(devicId)) {
                    s_msg="存在门锁入网";
                } else if ("010D".equals(devicId)) {
                    s_msg="存在门磁入网";
                } else if ("010E".equals(devicId)) {
                    s_msg="存在红外入网";
                } else if ("010C".equals(devicId)) {
                    s_msg="存在取电开关入网";
                } else if ("0002".equals(devicId)) {

                    s_msg="存在窗帘入网";

                } else if ("010F".equals(devicId)) {
                    s_msg="存在中央空调入网";
                } else if ("0020".equals(devicId)) {
                    s_msg="存在红外转发器入网";

                } else if ("0009".equals(devicId)) {
                    s_msg="存在智能插座入网";
                }
                final AlertDialog.Builder builder = new AlertDialog.Builder(AddSheBeiActivity.this);
                builder.setMessage(s_msg);
                builder.setTitle("提示");
                builder.setPositiveButton("确认", new Dialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        builder.create().dismiss();
                    }
                });
                if (flag1) {
                    builder.create().show();
                }
            }
        }
    };*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_she_bei);
       // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
      //  getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        flag1 = true;
        m = (MyApplication) getApplication();
        mac = new ArrayList<String>();
        GetDeviceList();
    }

    @Override
    protected void onStart() {
        info();
        super.onStart();
    }

    private void info() {

        TextView tv2= (TextView) findViewById(R.id.finish);
        tv2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent it=new Intent(AddSheBeiActivity.this,MainActivity.class);
                startActivity(it);
                finish();
            }
        });

        LinearLayout iv3 = (LinearLayout) findViewById(R.id.iv_mykaiguan);
        iv3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        la = AnimationUtils.loadAnimation(this, R.anim.anim_load_dialog);
        iv = (ImageView) findViewById(R.id.iv_addshebei);
        tv = (TextView) findViewById(R.id.tv_addshebei);
        tv_start = (TextView) findViewById(R.id.start);
        tv_select = (TextView) findViewById(R.id.select);
        tv_selected = (TextView) findViewById(R.id.selected_mac);
        tv_select2=(TextView) findViewById(R.id.select_mac1);
        tv_select.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                View V = View.inflate(AddSheBeiActivity.this, R.layout.style_dialog, null);
                MyDialog dialog = new MyDialog(AddSheBeiActivity.this, V, R.style.dialog, mac, tv_start, tv_selected,tv_select2, m.c, tv, iv, la);
                dialog.show();
                dialog.setCanceledOnTouchOutside(false);
            }
        });
        if(tv_select.getText().toString()!=null){
            tv_start.setClickable(true);
        }else{
            tv_start.setClickable(false);
        }
    }
    @Override
    protected void onDestroy() {
        flag1 = false;
        super.onDestroy();
    }



    public void GetDeviceList() {
            GateWay Gw = new GateWay();
            Gw.userId = m.userId;
            Gw.status = 1;
            String j = JSON.toJSONString(Gw);
            j = AESUtil.encrypt(j);
            HttpUtils httpUtils = new HttpUtils();
            RequestParams params = new RequestParams();
            params.setHeader("Content-Type", "application/json");
            try {
                params.setBodyEntity(new StringEntity(j, "utf-8"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/device/getDeviceList", params,
                    new RequestCallBack<String>() {

                        @Override
                        public void onFailure(HttpException arg0, String arg1) {

                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> arg0) {
                            if (arg0.result != null && !("".equals(arg0.result))) {
                                arg0.result = AESUtil.decrypt(arg0.result);
                                JSONObject jb = JSONObject.parseObject(arg0.result);
                                String status = jb.getString("status");
                                if ("0".equals(status)) {
                                    JSONArray ja = jb.getJSONArray("deviceList");
                                    m.m_gateway = (ArrayList<GateWay>) JSON.parseArray(ja.toString(), GateWay.class);
                                    if(m.m_gateway!=null) {
                                        for (int i = 0; i < m.m_gateway.size(); i++) {
                                            if (m.m_gateway.get(i).isOnline != null) {
                                                if (m.m_gateway.get(i).isOnline == true) {
                                                    mac.add(m.m_gateway.get(i).mac);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    });
    }

}
